#ifndef __NETSERVER_H__
#define __NETSERVER_H__

#pragma once

#include "nl.h"
#include "NetBase.h"
#include "osc/PacketListener.h"

class COgreEngine;
class UdpListeningReceiveSocket;

struct SClient
{
	SClient(void) : m_Socket(NL_INVALID), m_IsReady(false) {};
	NLsocket m_Socket;
	bool m_IsReady;

	void reset(void)
	{
		m_Socket = NL_INVALID;
		m_IsReady = false;
	}

	bool operator ==(NLsocket const &socket)
	{
		return (socket == m_Socket);
	}
};

class CNetServer : public CNetBase, public PacketListener
{
public:
	CNetServer(COgreEngine *pEngine);
	virtual ~CNetServer(void);

	void Create(void);
	void Destroy(void);

#ifdef __USE_HAWKTHREAD__
	static void * MainOSCLoop(void * pNetServer);
	static void * MainServerLoop(void * pNetServer);
#endif
#ifdef __USE_WIN32THREAD__
	static DWORD WINAPI MainOSCLoop( LPVOID pNetServer );
	static DWORD WINAPI MainServerLoop( LPVOID pNetServer );
#endif

	bool frameStarted(const FrameEvent& evt);
	bool frameEnded(const FrameEvent &evt);
	bool SendPacket(NLsocket Peer, void *pBuffer, unsigned int Size);
	void addCommand(SShared const &Shared);

    virtual void ProcessPacket( const char *data, int size, const IpEndpointName& remoteEndpoint );
	void setReady(bool val, NLsocket index = NL_INVALID);
	void SendSynchroPacket(void);

	Real getTime(void);

public:

	NLint m_NbClient;
	NLsocket m_pClient[MAX_CLIENTS];
	NLsocket m_pClientUnreliable[MAX_CLIENTS];
	NLint m_Group;

	UdpListeningReceiveSocket * m_pOSCReceiver;
	bool m_IsAllReady;
	bool m_IsServerReady;
	std::vector<SClient> m_Clients;

	static CNetServer * ms_pSingleton;

	bool m_setScene;
	bool m_serverIsReady;
};

#endif __NETSERVER_H__